package com.axin.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.mybatis.utils.PageUtils;
import com.axin.common.redis.utils.RedisUtils;
import com.axin.system.api.domain.SysDictData;
import com.axin.system.mapper.SysDictDataMapper;
import com.axin.system.service.ISysDictDataService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.axin.system.service.impl.SysDictTypeServiceImpl.getCacheKey;

/**
 * 字典数据 业务层处理
 *
 * @Author axin
 * @Date 2021/10/27 12:41
 **/
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType
     * @return
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        return baseMapper.selectDictDataByType(dictType);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return getById(dictCode);
    }


    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data) {
        int row = baseMapper.updateById(data);
        if (row > 0) {
            List<SysDictData> dictDataList = baseMapper.selectDictDataByType(data.getDictType());
            redisUtils.set(getCacheKey(data.getDictType()), JSONObject.toJSONString(dictDataList));
        }
        return row;
    }


    @Override
    public TableDataInfo<SysDictData> selectPageDictDataList(SysDictData dictData) {
        LambdaQueryWrapper<SysDictData> lqw = new LambdaQueryWrapper<SysDictData>()
                .eq(StrUtil.isNotBlank(dictData.getDictType()), SysDictData::getDictType, dictData.getDictType())
                .like(StrUtil.isNotBlank(dictData.getDictLabel()), SysDictData::getDictLabel, dictData.getDictLabel())
                .eq(StrUtil.isNotBlank(dictData.getStatus()), SysDictData::getStatus, dictData.getStatus())
                .orderByAsc(SysDictData::getDictSort);
        return PageUtils.buildDataInfo(page(PageUtils.buildPage(), lqw));
    }


    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data) {
        int row = baseMapper.insert(data);
        if (row > 0) {
            List<SysDictData> dictDataList = baseMapper.selectDictDataByType(data.getDictType());
            redisUtils.set(getCacheKey(data.getDictType()), JSONObject.toJSONString(dictDataList));
        }
        return row;
    }


    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     * @return 结果
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            SysDictData data = selectDictDataById(dictCode);
            removeById(dictCode);
            List<SysDictData> dictDataList = baseMapper.selectDictDataByType(data.getDictType());
            redisUtils.set(data.getDictType(), JSONObject.toJSONString(dictDataList));
        }
    }

}
